export const activityInitData = [
  {
    key: '1',
    name: 'John',
    age: 32,
    sex: '1',
    grade: '1',
  },
  {
    key: '2',
    name: 'Jim',
    age: 42,
    sex: '2',
    grade: '1',
  },
  {
    key: '3',
    name: 'Joe',
    age: 32,
    sex: '1',
    grade: '2',
  },
]

let searchBeforeData = activityInitData;

// 统一创建 action，并暴露
const actionDelType = 'DEL';
export const actionDel = (id) => ({
  type: actionDelType,
  payload: id
});

const actionSearchType = 'SEARCH';
export const actionSearch = (payload) => ({
  type: actionSearchType,
  payload
});

const actionResetType = 'RESET';
export const actionReset = () => ({
  type: actionResetType
});

export default function activityReducer(state, action) {
  switch (action.type) {
    case actionSearchType:
      let obj = [...state];
      // 搜索之前先保存数据
      searchBeforeData = [...state];
      if (action.payload.name) {
        obj = state.filter(item => item.name == action.payload.name)
      }
      if (action.payload.sex) {
        obj = obj.filter(item => item.sex == action.payload.sex)
      }
      if (action.payload.grade) {
        obj = obj.filter(item => item.grade == action.payload.grade)
      }
      console.log('传递的搜索参数：', obj);
      return obj;

    case actionDelType:
      return state.filter(item => item.key != action.payload);

    case actionResetType:
      return searchBeforeData;

    default:
      return state;
  }
}